#include "cloud_aopu.h"
#include "mqtt_config.h"

vdata_mqtt_gateway_info_t vdata_mqtt_gateway_info = {
    .account = "zhvdata",
    .sn = "zhvdata",
    .addr = "0",

    .rt = "/ge/mqtt_test/huida/0",
    .rf = "0",
    .gv = "zhvdata",
    .gm = "VDG411-00(03)",
    .gs = "zhvdata",
    .ghv = "V1.0.0",
    .gsv = "V1.0.0"
};

uint16_t vdata_mqtt_msg_id(void)
{
    static uint16_t msg_id = 0;
    return msg_id++;
}

char *vdata_get_time_stamp(void)
{
    static char time_string[50] = {0};
    time_t timep;
    struct tm *p;
    time(&timep);
    p = localtime(&timep); //"yyyy-MM-dd'T'HH:mm:ss.SSS[+-]HH:ss""2018-05-17T03:30:54+0000"
    sprintf(time_string, "%04d-%02d-%02dT%02d:%02d:%02d+0000", 1900 + p->tm_year, p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
    return time_string;
}

int vdata_register_req(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    char *string = NULL;
    cJSON *send, *node_h, *node_b;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  0x45); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号
        cJSON_AddStringToObject(node_h, "rt", gateway->rt);//响应消息回送 Topic，请求消息中必填

        cJSON_AddNumberToObject(node_b, "rf", 0x01);//注册标志位，现在不用
        cJSON_AddStringToObject(node_b, "gv", gateway->gv);//网关厂商
        cJSON_AddStringToObject(node_b, "gm", gateway->gm);//网关型号
        cJSON_AddStringToObject(node_b, "gs", gateway->gs);//网关序列号
        cJSON_AddStringToObject(node_b, "ghv", gateway->ghv);//网关硬件版本
        cJSON_AddStringToObject(node_b, "gsv", gateway->gsv);//网关软件版本

        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        return 1;
    }else{
        return -1;
    }
}

int vdata_unregister_req(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    char *string = NULL;
    cJSON *send, *node_h, *node_b;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  0x41); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号

        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        return 1;
    }else{
        return -1;
    }
}

int vdata_ping_req(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    char *string = NULL;
    cJSON *send, *node_h, *node_b;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  0x46); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号
        cJSON_AddStringToObject(node_h, "rt", gateway->rt);//响应消息回送 Topic，请求消息中必填

        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        return 1;
    }else{
        return -1;
    }
}

int vdata_get_device_list_resp(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    int i = 0;
    char *string = NULL;
    cJSON *send, *node_h, *node_b, *node_dvl, *node_dvn;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }
        node_dvl = cJSON_CreateArray();
        if(!node_dvl){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            cJSON_Delete(node_b);
            return -1;
        }
        node_dvn = cJSON_CreateObject();
        if(!node_dvn){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            cJSON_Delete(node_b);
            cJSON_Delete(node_dvl);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  0x95); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号

        for(i = 0; i < 1; i++){
            cJSON_AddNumberToObject(node_dvn, "da", 0x01);//设备地址
            cJSON_AddNumberToObject(node_dvn, "ds", 0x01);//设备状态，0：离线；1：在线
            cJSON_AddItemToArray(node_dvl, node_dvn);
        }
        cJSON_AddItemToObject(node_b, "dvl", node_dvl);
        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        return 1;
    }else{
        return -1;
    }
}

int vdata_report_device_status_notif(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    int i = 0;
    char *string = NULL;
    cJSON *send, *node_h, *node_b, *node_dvl, *node_dvn;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }
        node_dvl = cJSON_CreateArray();
        if(!node_dvl){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            cJSON_Delete(node_b);
            return -1;
        }
        node_dvn = cJSON_CreateObject();
        if(!node_dvn){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            cJSON_Delete(node_b);
            cJSON_Delete(node_dvl);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  6); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号

        for(i = 0; i < 1; i++){
            cJSON_AddNumberToObject(node_dvn, "da", 0x01);//设备地址
            cJSON_AddNumberToObject(node_dvn, "ds", 0x01);//设备状态，0：离线；1：在线
            cJSON_AddNumberToObject(node_dvn, "cf", 0x02);//增删标志，0：删除；1：增加；2：不变
            cJSON_AddItemToArray(node_dvl, node_dvn);
        }
        cJSON_AddItemToObject(node_b, "dvl", node_dvl);
        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        return 1;
    }else{
        return -1;
    }
}

int vdata_report_data_notif1(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    int i = 0, count = 0;
    char *string = NULL;
    cJSON *send, *node_h, *node_b, *node_dl;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }
        node_dl = cJSON_CreateArray();
        if(!node_dl){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            cJSON_Delete(node_b);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  5); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号

        cJSON_AddNumberToObject(node_b, "rf", 2);
        cJSON_AddStringToObject(node_b, "ts", vdata_get_time_stamp());
        cJSON_AddNumberToObject(node_b, "tc", 1);

        //"dl"
        for(i = 0; i < MQTT_MODBUS_CONFIG_MAX; i ++){
            if(!node[i].used || node[i].sent){
                continue;
            }
            cJSON_AddItemToArray(node_dl, cJSON_CreateNumber(node[i].reg_value));
            node[i].sent = 1;
            if(++count > 50){
                break;
            }
        }

        cJSON_AddItemToObject(node_b, "dl", node_dl);
        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        return 1;
    }else{
        return -1;
    }
}

int vdata_report_data_notif2(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    int i = 0, count = 0;
    char *string = NULL;
    cJSON *send, *node_h, *node_b, *node_tl, *node_tln, *node_dl;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }
        node_tl = cJSON_CreateArray();
        if(!node_tl){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            cJSON_Delete(node_b);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  5); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号

        cJSON_AddNumberToObject(node_b, "rf", 4);
        cJSON_AddStringToObject(node_b, "ts", vdata_get_time_stamp());
        for(i = 0; i < 4; i++){//"tl"
            node_tln = cJSON_CreateObject();
            if(!node_tln){
                cJSON_Delete(send);
                cJSON_Delete(node_h);
                cJSON_Delete(node_b);
                cJSON_Delete(node_tl);
                return -1;
            }
            node_dl = cJSON_CreateArray();
            if(!node_dl){
                cJSON_Delete(send);
                cJSON_Delete(node_h);
                cJSON_Delete(node_b);
                cJSON_Delete(node_tl);
                cJSON_Delete(node_tln);
                return -1;
            }
            cJSON_AddNumberToObject(node_tln, "tc", 4);
            cJSON_AddNumberToObject(node_tln, "da", 4);
            cJSON_AddNumberToObject(node_tln, "to", 4);

            //"dl"
            for(i = 0; i < MQTT_MODBUS_CONFIG_MAX; i ++){
                if(!node[i].used || node[i].sent){
                    continue;
                }
                cJSON_AddItemToArray(node_dl, cJSON_CreateNumber(node[i].reg_value));
                node[i].sent = 1;
                if(++count > 150){
                    break;
                }
            }
            cJSON_AddItemToObject(node_tln, "dl", node_dl);
            cJSON_AddItemToArray(node_tl, node_tln);

        }
        cJSON_AddItemToObject(node_b, "tl", node_tl);
        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        return 1;
    }else{
        return -1;
    }
}

int vdata_report_data_notif3(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    int i = 0, count = 0;
    char *string = NULL, tagname[20];
    cJSON *send, *node_h, *node_b, *node_dl;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }
        node_dl = cJSON_CreateObject();
        if(!node_dl){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            cJSON_Delete(node_b);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  5); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号

        cJSON_AddNumberToObject(node_b, "rf", 5);
        cJSON_AddStringToObject(node_b, "ts", vdata_get_time_stamp());

        //"dl"
        for(i = 0; i < MQTT_MODBUS_CONFIG_MAX; i ++){
            if(!node[i].used || node[i].sent){
                continue;
            }
            switch (node[i].reg_type) {
                case 1:
                    sprintf(tagname, "1X%d", node[i].reg_addr);
                    cJSON_AddItemToObject(node_dl, tagname, cJSON_CreateNumber(node[i].reg_value));
                    break;
                case 2:
                    sprintf(tagname, "2X%d", node[i].reg_addr);
                    cJSON_AddItemToObject(node_dl, tagname, cJSON_CreateNumber(node[i].reg_value));
                    break;
                case 3:
                    sprintf(tagname, "3X%d", node[i].reg_addr);
                    cJSON_AddItemToObject(node_dl, tagname, cJSON_CreateNumber(node[i].reg_value));
                    break;
                case 4:
                    sprintf(tagname, "4X%d", node[i].reg_addr);
                    cJSON_AddItemToObject(node_dl, tagname, cJSON_CreateNumber(node[i].reg_value));
                    break;
                default:
                    break;
            }
            node[i].sent = 1;
            if(++count > 50){
                break;
            }
        }

        cJSON_AddItemToObject(node_b, "dl", node_dl);
        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        if(i < MQTT_MODBUS_CONFIG_MAX){
            return 1;
        }else{
            return 0;
        }
    }else{
        return -1;
    }
}

int vdata_report_data_notif4(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    int i = 0, count = 0;
    char *string = NULL, tagname[20];
    cJSON *send, *node_h, *node_b, *node_dvl, *node_dvln, *node_dl;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }
        node_dvl = cJSON_CreateArray();
        if(!node_dvl){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            cJSON_Delete(node_b);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  5); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号

        cJSON_AddNumberToObject(node_b, "rf", 6);
        cJSON_AddStringToObject(node_b, "ts", vdata_get_time_stamp());
        for(i = 0; i < 1; i++){//"dvl"
            node_dvln = cJSON_CreateObject();
            if(!node_dvln){
                cJSON_Delete(send);
                cJSON_Delete(node_h);
                cJSON_Delete(node_b);
                cJSON_Delete(node_dvl);
                return -1;
            }
            node_dl = cJSON_CreateObject();
            if(!node_dl){
                cJSON_Delete(send);
                cJSON_Delete(node_h);
                cJSON_Delete(node_b);
                cJSON_Delete(node_dvl);
                cJSON_Delete(node_dvln);
                return -1;
            }
            cJSON_AddNumberToObject(node_dvln, "da", i);
            cJSON_AddNumberToObject(node_dvln, "to", 0);
            //"dl"
            //"dl"
            for(i = 0; i < MQTT_MODBUS_CONFIG_MAX; i ++){
                if(!node[i].used || node[i].sent){
                    continue;
                }
                switch (node[i].reg_type) {
                    case 1:
                        sprintf(tagname, "1X%d", node[i].reg_addr);
                        cJSON_AddItemToObject(node_dl, tagname, cJSON_CreateNumber(node[i].reg_value));
                        break;
                    case 2:
                        sprintf(tagname, "2X%d", node[i].reg_addr);
                        cJSON_AddItemToObject(node_dl, tagname, cJSON_CreateNumber(node[i].reg_value));
                        break;
                    case 3:
                        sprintf(tagname, "3X%d", node[i].reg_addr);
                        cJSON_AddItemToObject(node_dl, tagname, cJSON_CreateNumber(node[i].reg_value));
                        break;
                    case 4:
                        sprintf(tagname, "4X%d", node[i].reg_addr);
                        cJSON_AddItemToObject(node_dl, tagname, cJSON_CreateNumber(node[i].reg_value));
                        break;
                    default:
                        break;
                }
                node[i].sent = 1;
                if(++count > 50){
                    break;
                }
            }

            //"tagN"...
            cJSON_AddItemToObject(node_dvln, "dl", node_dl);
            cJSON_AddItemToArray(node_dvl, node_dvln);
        }
        cJSON_AddItemToObject(node_b, "dvl", node_dvl);
        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        return 1;
    }else{
        return -1;
    }
}

int vdata_send_sevice_command_resp1(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    char *string = NULL;
    cJSON *send, *node_h, *node_b, *node_dl, *node_dln;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }
        node_dl = cJSON_CreateArray();
        if(!node_dl){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            cJSON_Delete(node_b);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  149); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号

        cJSON_AddNumberToObject(node_b, "cf", 2);
        cJSON_AddNumberToObject(node_b, "cc", 0);
        cJSON_AddNumberToObject(node_b, "code", 0);
        cJSON_AddStringToObject(node_b, "desc", "ok");

        while(gateway->node){//"dl"
            node_dln = cJSON_CreateObject();
            if(!node_dln){
                cJSON_Delete(send);
                cJSON_Delete(node_h);
                cJSON_Delete(node_b);
                cJSON_Delete(node_dl);
                return -1;
            }
            cJSON_AddItemToObject(node_dln, "dov", cJSON_CreateNumber(0));
            cJSON_AddItemToObject(node_dln, "dnv", cJSON_CreateNumber(gateway->node->valueint));
            cJSON_AddItemToArray(node_dl, node_dln);
            gateway->node = gateway->node->next;
        }
        cJSON_AddItemToObject(node_b, "dl", node_dl);
        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        return 1;
    }else{
        return -1;
    }
}

int vdata_send_sevice_command_resp2(vdata_mqtt_gateway_info_t *gateway, char *out)
{
    int r_index = 0, w_index = 0, addr = 0;
    char *string = NULL;
    cJSON *send, *node_h, *node_b, *node_dl, *node_dln;
    mqtt_modbus_config_t *node = mqtt_modbus_config;

    gateway = gateway;
    send = cJSON_CreateObject();
    if(send){
        node_h = cJSON_CreateObject();
        if(!node_h){
            cJSON_Delete(send);
            return -1;
        }
        node_b = cJSON_CreateObject();
        if(!node_b){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            return -1;
        }
        node_dl = cJSON_CreateObject();
        if(!node_dl){
            cJSON_Delete(send);
            cJSON_Delete(node_h);
            cJSON_Delete(node_b);
            return -1;
        }

        cJSON_AddNumberToObject(node_h, "T",  149); //消息类型的十进制编码
        cJSON_AddNumberToObject(node_h, "ID", vdata_mqtt_msg_id());//消息流水号

        cJSON_AddNumberToObject(node_b, "cf", 3);
        cJSON_AddNumberToObject(node_b, "cc", 0);
        cJSON_AddNumberToObject(node_b, "code", 0);
        cJSON_AddStringToObject(node_b, "desc", "ok");

        while(gateway->node){//"dl"
            node_dln = cJSON_CreateObject();
            if(!node_dln){
                cJSON_Delete(send);
                cJSON_Delete(node_h);
                cJSON_Delete(node_b);
                cJSON_Delete(node_dl);
                return -1;
            }
            if(!strncmp("1X", gateway->node->string, 2)){
                addr = atoi(&gateway->node->string[2]);
                r_index = mqtt_modbus_config_get_index(1, 1, addr);
                if(r_index >= 0){
                    mqtt_modbus_write_cmd[w_index].type = 1;
                    mqtt_modbus_write_cmd[w_index].addr = addr;
                    mqtt_modbus_write_cmd[w_index].data = gateway->node->valueint;

                    cJSON_AddItemToObject(node_dln, "dov", cJSON_CreateNumber(node[r_index].reg_value));
                    cJSON_AddItemToObject(node_dln, "dnv", cJSON_CreateNumber(gateway->node->valueint));
                    node[r_index].reg_value = gateway->node->valueint;
                }

            }else if(!strncmp("2X", gateway->node->string, 2)){
                addr = atoi(&gateway->node->string[2]);
                r_index = mqtt_modbus_config_get_index(1, 2, addr);
                if(r_index >= 0){
                    cJSON_AddItemToObject(node_dln, "dov", cJSON_CreateNumber(node[r_index].reg_value));
                    cJSON_AddItemToObject(node_dln, "dnv", cJSON_CreateNumber(gateway->node->valueint));
                    node[r_index].reg_value = gateway->node->valueint;
                }
            }else if(!strncmp("3X", gateway->node->string, 2)){
                addr = atoi(&gateway->node->string[2]);
                r_index = mqtt_modbus_config_get_index(1, 3, addr);
                if(r_index >= 0){
                    cJSON_AddItemToObject(node_dln, "dov", cJSON_CreateNumber(node[r_index].reg_value));
                    cJSON_AddItemToObject(node_dln, "dnv", cJSON_CreateNumber(gateway->node->valueint));
                    node[r_index].reg_value = gateway->node->valueint;
                }
            }else if(!strncmp("4X", gateway->node->string, 2)){
                addr = atoi(&gateway->node->string[2]);
                r_index = mqtt_modbus_config_get_index(1, 4, addr);
                if(r_index >= 0){
                    mqtt_modbus_write_cmd[w_index].type = 4;
                    mqtt_modbus_write_cmd[w_index].addr = addr;
                    mqtt_modbus_write_cmd[w_index].data = gateway->node->valueint;
                    cJSON_AddItemToObject(node_dln, "dov", cJSON_CreateNumber(node[r_index].reg_value));
                    cJSON_AddItemToObject(node_dln, "dnv", cJSON_CreateNumber(gateway->node->valueint));
                    node[r_index].reg_value = gateway->node->valueint;
                }
            }
            cJSON_AddItemToObject(node_dl, gateway->node->string, node_dln);
            gateway->node = gateway->node->next;
        }
        cJSON_AddItemToObject(node_b, "dl", node_dl);
        cJSON_AddItemToObject(send, "h", node_h);
        cJSON_AddItemToObject(send, "b", node_b);

        string = cJSON_Print(send);
        cJSON_Minify(string);
        if(string){
            sprintf(out , "%s", string);
        }
        cJSON_Delete(send);
        free(string);
        return 1;
    }else{
        return -1;
    }
}
